package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.CheckGroup;
import com.xrit.sunnyday.service.CheckgroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Daoying
 * creationTime: 2022/10/18 17:27
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {
    @Reference
    CheckgroupService checkgroupService;

    //查询所有检查组
    @GetMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkgroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }


    //分页查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkgroupService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    //添加
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        // 调用业务服务
        checkgroupService.add(checkGroup, checkitemIds);
        // 响应结果
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //删除
    @PostMapping("/deleteById")
    public Result deleteById(int id) {
        checkgroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //通过id 查询检查组
    @GetMapping("findById")
    public Result findById(int checkGroupId) {
        CheckGroup checkGroup = checkgroupService.findById(checkGroupId);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    //通过检查组id查询选中的检查项id
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(int checkGroupId) {
        List<Integer> checkItemIds = checkgroupService.findCheckItemIdsByCheckGroupId(checkGroupId);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkItemIds);
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        checkgroupService.update(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
}
